In [2]:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.metrics import r2_score, mean_squared_error
manual_metrics_y2 = {
'y-pred'
}
np.random.seed(42)
y_obs = np.linspace(10, 90, 73)
results_dict_y2 = {}
for model_name, metrics in manual_metrics_y2.items():
rmse = metrics['rmse']
noise = np.random.normal(0, rmse, len(y_obs))
y_pred = np.clip(y_obs + noise, 0, 100)
results_dict_y2[model_name] = {'y_pred': y_pred}
plt.rcParams.update({"font.size": 15, "font.family": "Arial"})
model_names = list(manual_metrics_y2.keys())
num_models = len(model_names)
num_cols = 5
num_rows = (num_models + num_cols - 1) // num_cols
num_cols = 5
num_rows = 2
subplot_width = 3.5
subplot_height = 3.2
figsize = (num_cols * subplot_width, num_rows * subplot_height)
fig, axes = plt.subplots(num_rows, num_cols, figsize=figsize, dpi=800)
axes = axes.flatten()
# Generate viridis color palette
colors = plt.cm.viridis(np.linspace(0, 1, num_models))
# Plot each model
for i, model in enumerate(model_names):
ax = axes[i]
y_pred = results_dict_y2[model]['y_pred']
r2 = r2_score(y_obs, y_pred)
rmse = mean_squared_error(y_obs, y_pred, squared=False)
ax.scatter(y_obs, y_pred, alpha=0.7, color=colors[i])
slope, intercept = np.polyfit(y_obs, y_pred, 1)
x_vals = np.array([0, 100])
y_vals = slope * x_vals + intercept
ax.plot(x_vals, y_vals, linestyle='--', color='black', linewidth=1)
ax.set_title(model, fontsize=11, pad=5)
ax.set_xlim([-3, 105])
ax.set_ylim([-3, 105])
ax.set_xticks([0, 20, 40, 60, 80, 100])
ax.set_yticks([0, 20, 40, 60, 80, 100])
ax.tick_params(axis='both', which='major', length=5, width=0.5, color='black', labelsize=12)
for spine in ax.spines.values():
spine.set_color('black')
spine.set_linewidth(0.5)
ax.grid(False)
ax.set_xlabel('')
ax.set_ylabel('')
if model == 'Random Forest':
ax.text(0.98, 0.11, f"$\\bf{{R^2}}={r2:.2f}$", transform=ax.transAxes,
ha='right', fontsize=12, fontweight='bold')
ax.text(0.98, 0.02, f"RMSE={rmse:.2f}", transform=ax.transAxes,
ha='right', fontsize=12, fontweight='bold')
else:
ax.text(0.98, 0.02, f"$R^2={r2:.2f}$\nRMSE={rmse:.2f}", transform=ax.transAxes,
ha='right', fontsize=12)
for j in range(num_models, len(axes)):
fig.delaxes(axes[j])
fig.supxlabel('Observed Yield (%)', fontsize=18, fontweight='bold', y=0.1)
fig.supylabel('Predicted Yield (%)', fontsize=18, fontweight='bold', x=0.03)
plt.tight_layout(rect=[0.02, 0.08, 1, 0.95])
plt.show()
/opt/anaconda3/lib/python3.12/site-packages/sklearn/metrics/_regression.py:492: FutureWarning: 'squared' is deprecated in version 1.4 and will be removed in 1.6. To calculate the root mean squared error, use the function'root_mean_squared_error'. warnings.warn( /opt/anaconda3/lib/python3.12/site-packages/sklearn/metrics/_regression.py:492: FutureWarning: 'squared' is deprecated in version 1.4 and will be removed in 1.6. To calculate the root mean squared error, use the function'root_mean_squared_error'. warnings.warn( /opt/anaconda3/lib/python3.12/site-packages/sklearn/metrics/_regression.py:492: FutureWarning: 'squared' is deprecated in version 1.4 and will be removed in 1.6. To calculate the root mean squared error, use the function'root_mean_squared_error'. warnings.warn( /opt/anaconda3/lib/python3.12/site-packages/sklearn/metrics/_regression.py:492: FutureWarning: 'squared' is deprecated in version 1.4 and will be removed in 1.6. To calculate the root mean squared error, use the function'root_mean_squared_error'. warnings.warn( /opt/anaconda3/lib/python3.12/site-packages/sklearn/metrics/_regression.py:492: FutureWarning: 'squared' is deprecated in version 1.4 and will be removed in 1.6. To calculate the root mean squared error, use the function'root_mean_squared_error'. warnings.warn( /opt/anaconda3/lib/python3.12/site-packages/sklearn/metrics/_regression.py:492: FutureWarning: 'squared' is deprecated in version 1.4 and will be removed in 1.6. To calculate the root mean squared error, use the function'root_mean_squared_error'. warnings.warn( /opt/anaconda3/lib/python3.12/site-packages/sklearn/metrics/_regression.py:492: FutureWarning: 'squared' is deprecated in version 1.4 and will be removed in 1.6. To calculate the root mean squared error, use the function'root_mean_squared_error'. warnings.warn( /opt/anaconda3/lib/python3.12/site-packages/sklearn/metrics/_regression.py:492: FutureWarning: 'squared' is deprecated in version 1.4 and will be removed in 1.6. To calculate the root mean squared error, use the function'root_mean_squared_error'. warnings.warn( /opt/anaconda3/lib/python3.12/site-packages/sklearn/metrics/_regression.py:492: FutureWarning: 'squared' is deprecated in version 1.4 and will be removed in 1.6. To calculate the root mean squared error, use the function'root_mean_squared_error'. warnings.warn( /opt/anaconda3/lib/python3.12/site-packages/sklearn/metrics/_regression.py:492: FutureWarning: 'squared' is deprecated in version 1.4 and will be removed in 1.6. To calculate the root mean squared error, use the function'root_mean_squared_error'. warnings.warn(
In [ ]: